<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function minSubArrayLen(target, nums) {
      let res = Infinity;
      let len = nums.length;
      for (let i = 0, j = 0, cur = 0; i < len; i++) {
        //累加
        cur += nums[i];
        //判断并移动左边
        while (cur >= target) {
          //更新res
          res = Math.min(res, i - j + 1)
          cur -= nums[j];
          j++;
        }
      }
      //判断是否一直没有更新res，没有更新就返回01
      return res === Infinity ? 0 : res;
    }
    console.log(minSubArrayLen(target = 7, nums = [2, 3, 1, 2, 4, 3]));
  </script>
</body>

</html>